package com.xaicode.auth.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xaicode.auth.annotation.Log;
import com.xaicode.auth.domain.Result;
import com.xaicode.auth.domain.params.IdsParam;
import com.xaicode.auth.entity.SysDict;
import com.xaicode.auth.enums.BusinessType;
import com.xaicode.auth.security.domain.params.SysDictQueryParam;
import com.xaicode.auth.service.ISysDictService;
import com.xaicode.auth.utils.GlobalValidator;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 字典接口
 * 
 * @date 2024/12/19
 */
@RestController
@RequestMapping("/api/system/dict")
public class SysDictController {

    @Autowired
    private ISysDictService sysDictService;


    @Log(title = "查看字典列表", businessType = BusinessType.QUERY, isSaveResponseData = false)
    @SaCheckPermission("system:dict:list")
    @PostMapping("/page")
    public Result<?> page(@RequestBody SysDictQueryParam param) {
        PageInfo<SysDict> pageInfo = sysDictService.queryPage(param);
        return Result.ok(pageInfo);
    }

    @Log(title = "新增字典", businessType = BusinessType.INSERT)
    @SaCheckPermission("system:dict:add")
    @PostMapping("/create")
    public Result<?> create(@RequestBody SysDict entity) {
        sysDictService.save(entity);
        return Result.ok();
    }

    @Log(title = "修改字典", businessType = BusinessType.UPDATE)
    @SaCheckPermission("system:dict:edit")
    @PostMapping("/update")
    public Result<?> update(@RequestBody SysDict entity) {
        GlobalValidator.notNull(entity.getId(), "id不能为空");
        sysDictService.updateDict(entity);
        return Result.ok();
    }

    @Log(title = "删除字典", businessType = BusinessType.DELETE)
    @SaCheckPermission("system:dict:del")
    @PostMapping("/delete")
    public Result<?> delete(@RequestBody IdsParam<Integer> ids) {
        sysDictService.removeDictByIds(ids.getIds());
        return Result.ok();
    }

}
